<template>
	<view class="withdrawal">
		<u-navbar
			title="分润提现记录" 
			left-text="" 
			right-text="筛选" 
			@leftClick="onClickBack" 
			@rightClick="onClickRight"
			:placeholder="true"
			:fixed="true"
			:safeAreaInsetTop="true"
			class="nav"
		></u-navbar>
		<u-popup :show="filterTimer" @close="closeFilterTimer" mode="top" :safeAreaInsetTop="true">
			<view class="filterTimer">
				<view class="plr">
					<view class="w-flex w-flex-between">
						<view class="startTime" @click="showTime = true" :style="showCurrStartTime !== '请选择日期'?'color:#000;font-size:14px;':''">
							<text>{{showCurrStartTime}}</text>
							<u-icon name="arrow-down" color="#b3b3b3" size="14"></u-icon>
						</view>
						<text style="margin: 0 10px;">至</text>
						<view class="startTime" @click="showEndTime = true" :style="showCurrEndTime !== '请选择日期'?'color:#000;font-size:14px;':''">
							<text>{{showCurrEndTime}}</text>
							<u-icon name="arrow-down" color="#b3b3b3" size="14"></u-icon>
						</view>
					</view>
					<view>
						<u-datetime-picker
							:show="showTime"
							:value="startTime"
							mode="date"
							@cancel="cancel"
							@confirm="confirmTime"
							:maxDate="maxDate"
							:formatter="formatter"
						></u-datetime-picker>
						<u-datetime-picker
							:show="showEndTime"
							:value="endTime"
							mode="date"
							@cancel="cancelEndTime"
							@confirm="confirmEndTime"
							:maxDate="maxDate"
							:formatter="formatter"
						></u-datetime-picker>
					</view>
					<view class="w-flex w-flex-between w-mt-15 timerChoice">
						<text class="dateStyle" :class="activeDate === 0?'activeDate':''" @click="timerToday">近三个月</text>
						<text class="dateStyle" :class="activeDate === 1?'activeDate':''" @click="timerWeek">近半年</text>
						<text class="dateStyle" :class="activeDate === 2?'activeDate':''" @click="timerMonth">近一年</text>
					</view>
					<view class="w-mt-20">
						<view>
							选择状态
						</view>
						<view class="w-mt-15">
							<u-checkbox-group
								v-model="MerchantStatus"
								placement="row"
								@change="MerchantStatusChange"
								style="display: flex;flex-wrap: wrap;"
							>
								<u-checkbox
									:customStyle="{marginBottom: '8px'}"
									v-for="(item, index) in MerchantStatusList"
									:key="index"
									:label="item.name"
									:name="item.id"
									style="width: 33%;"
									size="14"
									labelSize="12"
									labelColor="#7e7e7e"
								>
								</u-checkbox>
							</u-checkbox-group>
						</view>
					</view>
				</view>
				
				<view class="w-flex w-flex-between w-mt-20 bottomBtn">
					<text class="txtBtn" @click="closeFilterTimer">取消</text>
					<text class="txtBtn" style="color: #1678ff;" @click="dateFilter">确定</text>
				</view>
			</view>
		</u-popup>
		
		<view class="w-content">
			<!-- <view class="w-flex">
				<text @click="changeDate" style="color: #000;margin-right: 10rpx;">{{choiceDate}}</text>
				<u-icon name="arrow-down-fill" size="10" color="#bfbfbf" @click="changeDate"></u-icon>
				<u-datetime-picker
					:show="showchoiceDate"
					:value="choiceDialogDate"
					mode="year-month"
					@cancel="cancelchoiceDate"
					@confirm="confirmchoiceDate"
					:maxDate="choicemaxDate"
					:formatter="formatter"
				></u-datetime-picker>
			</view> -->
			<view class="w-flex w-flex-between w-font-12" style="color: #898989;">
				<text>累计提取总额：{{all_money || 0}}</text>
				<!-- <text>打款总额：{{totalPay}}</text> -->
			</view>
			
		</view>
		<view v-if="dataList.length > 0">
			<view class="cu-list menu-avatar">
				<view class="cu-item cur"  v-for="(item, index) in dataList" :key="index">
					<view class="cu-avatar radius lg">
						<view>
							<u-avatar src="../../static/icon/logo.png" size="48" shape="square"></u-avatar>
						</view>
					</view>
					<view class="content">
						<view>
							<view class="text-cut">{{item.orderid}}</view>
						</view>
						<view class="text-gray text-sm flex">
							<view class="text-cut">{{item.createtime}}</view>
						</view>
					</view>
					<view class="action">
						<view class="main-color text-xl">{{item.money}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="listItem" v-else>
			<u-empty
				mode="data"
				icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
		</view>
		<u-toast ref="uToast"></u-toast>
		<u-loadmore :status="status" class="w-pb-10" v-if="dataList.length >= 10"/>
	</view>
</template>

<script>
import {getPreMonth, format} from  "@/util/common.js"
export default {
	data() {
		return {
			filterTimer: false,
			showTime: false,
			showEndTime: false,
			startTime: Number(new Date().getTime() - 3600 * 1000 * 24 * 31),
			endTime: Number(new Date()),
			maxDate: Number(new Date()),
			showCurrEndTime: '请选择日期',
			showCurrStartTime: '请选择日期',
			MerchantStatus: ['created', 'successed', 'rejected'],
			MerchantStatusList: [
				{
					id: 'created',
					name: '未审核'
				},
				{
					id: 'successed',
					name: '审核通过'
				},
				{
					id: 'rejected',
					name: '审核不通过'
				}
			],
			choiceDate: '2021年11月',
			showchoiceDate: false,
			choiceDialogDate: Number(new Date()),
			choicemaxDate: Number(new Date()),
			activeDate: 2,
			totalPay: 0,
			format,
			dataList: [],
			all_money: '',
			status: 'loadmore',
			page: 1,
			closeRequest: false
		}
	},
	onLoad() {
		this.choiceDate = this.formatTimer(this.choiceDialogDate, 'YMD')
		this.showCurrStartTime = getPreMonth(12)
		this.showCurrEndTime = this.formatTimer(Number(new Date()))
		this.initDataList(this.page)
	},
	onReachBottom () {
		if (this.dataList.length >= 10) {
			this.status = 'loading';
			this.page = ++ this.page;
			setTimeout(() => {
				this.initDataList(this.page)
				if (this.closeRequest) this.status = 'nomore';
				else this.status = 'loading';
			}, 1500)
		}
	},
	methods: {
		async initDataList (page) {
			if (this.closeRequest) {
				this.$refs.uToast.show({
					type: 'default',
					title: '默认主题',
					message: "没有更多了",
				})
				this.status = 'nomore'
				return
			}
			let params = {
				pn: page,
				page_size: 10,
				s_time: this.showCurrStartTime + ' '+ '00:00:00',
				e_time: this.showCurrEndTime + ' '+ '23:59:59',
				// status: this.MerchantStatus
			}
			let str = this.MerchantStatus.toString()
			let urlStatus = ''
			let urlStatus1 = ''
			let urlStatus2 = ''
			if (str.indexOf('created') !== -1) {
				urlStatus = 'status[0]=created'
			}
			if (str.indexOf('successed') !== -1) {
				urlStatus1 = 'status[1]=successed'
			}
			if (str.indexOf('rejected') !== -1) {
				urlStatus2 = 'status[2]=rejected'
			}
			const res = await this.$API({url: '/api/user/withdrawlog?'+urlStatus+'&'+urlStatus1+'&'+urlStatus2, method:"GET", data: params})
			if (res.code === 1) {
				this.all_money = res.data.all_money
				if (res.data.list.length > 0) {
					for (let item of res.data.list) {
						this.dataList.push(item)
					}
					this.status = 'loadmore'
				} else {
					if (page > 1) {
						this.status = 'nomore'
						this.$refs.uToast.show({
							type: 'default',
							title: '默认主题',
							message: "没有更多了",
						})
						this.closeRequest = true
					}
					
				}
			}
		},
		// 返回页面
		onClickBack () {
			uni.navigateBack()
		},
		// 筛选日期
		onClickRight(){
			this.filterTimer = true
		},
		cancel () {
			this.showTime = false
		},
		cancelEndTime () {
			this.showEndTime = false
		},
		confirmEndTime (date) {
			this.showEndTime = false
			this.showCurrEndTime = this.formatTimer(date.value)
		},
		confirmTime (date) {
			this.showCurrStartTime = this.formatTimer(date.value)
			this.showTime = false
		},
		formatter(type, value) {
			if (type === 'year') {
					return `${value}年`
			}
			if (type === 'month') {
					return `${value}月`
			}
			if (type === 'day') {
					return `${value}日`
			}
			return value
		},
		formatTimer(time, type) {
			var time = new Date(time);
			var y = time.getFullYear();
			var m = time.getMonth()+1;
			var d = time.getDate();
			var h = time.getHours();
			var mm = time.getMinutes();
			var s = time.getSeconds();
			if (type === 'YMD') {
				return y+'年'+this.add0(m)+'月';
			}
			return y+'-'+this.add0(m)+'-'+this.add0(d);
		},
		add0(m){return m<10?'0'+m:m },
		timerToday () {
			this.showCurrStartTime = getPreMonth(3);
			this.showCurrEndTime = this.formatTimer(Number(new Date()));
			this.activeDate = 0
		},
		timerWeek () {
			this.showCurrStartTime = getPreMonth(6);
			this.showCurrEndTime = this.formatTimer(Number(new Date()));
			this.activeDate = 1
		},
		timerMonth () {
			this.showCurrStartTime = getPreMonth(12);
			this.showCurrEndTime = this.formatTimer(Number(new Date()));
			this.activeDate = 2
		},
		MerchantStatusChange (list) {
			this.MerchantStatus = list
			console.log(list)
		},
		closeFilterTimer () {
			this.filterTimer = false
		},
		changeDate (){
			this.showchoiceDate = true
		},
		cancelchoiceDate () {
			this.showchoiceDate = false
		},
		confirmchoiceDate (date) {
			this.choiceDate = this.formatTimer(date.value, 'YMD')
			this.showchoiceDate = false
		},
		dateFilter () {
			this.filterTimer = false
			this.dataList = []
			this.page = 1
			this.closeRequest = false
			this.initDataList(this.page)
		},
	}
}
</script>

<style lang="scss" scoped>
.withdrawal {
	position: absolute;
	width: 100%;
	height: 100%;
	background: #FFFFFF;
	.nav {
		/deep/ .u-navbar__content {
			z-index: 20000;
		}
	}
	.filterTimer {
		background: #FFFFFF;
		padding-top: 20rpx;
		.startTime {
			background: #F1F1F1;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #b3b3b3;
			padding: 0 20rpx;
			width: 320rpx;
			height: 60rpx;
			border-radius: 10rpx;
			font-size: 12px;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		
	}

	.w-content {
		padding: 30rpx 20rpx;
		background: #F1F1F1;
	}
	.listItem {
		background: #FFFFFF;
		padding: 10rpx 20rpx;
	}
}
.u-popup .u-slide-down-enter-active {
	top: 0 !important;
}

</style>
